1 00:00:00,050 --> 00:00:00,800 Welcome back. 2 00:00:00,800 --> 00:00:02,570 I hope the quiz went well for you. 3 00:00:02,570 --> 00:00:03,620 I hope you rocked it. 4 00:00:03,620 --> 00:00:08,990 And now we're going to work on another little project, or practicing with our better twin module by 5 00:00:08,990 --> 00:00:11,990 creating a door that has a proximity prompt. 6 00:00:11,990 --> 00:00:18,560 And when you interact with it, the door opens, and when you interact with it again, the door closes. 7 00:00:18,560 --> 00:00:24,140 So what I've done is I've created a folder in the workspace called doors, and I've placed this sliding 8 00:00:24,140 --> 00:00:25,010 door inside of it. 9 00:00:25,010 --> 00:00:30,270 You're going to be able to grab this sliding door as a resource attached to this lecture. 10 00:00:30,270 --> 00:00:33,090 So go to the resources section for this lecture. 11 00:00:33,090 --> 00:00:37,980 Grab this model and insert it into your game and place it in a folder called doors. 12 00:00:37,980 --> 00:00:44,490 Because what you're going to do is I want you to go through all of the doors in the map and basically 13 00:00:44,490 --> 00:00:49,410 listen to all their proximity prompts and tween them when the prompts are interacted with. 14 00:00:49,410 --> 00:00:51,570 So I should be able to interact. 15 00:00:51,570 --> 00:00:53,490 The prompt should disappear. 16 00:00:53,490 --> 00:00:58,020 the door should tween and when it's done tweening the prompt should reappear. 17 00:00:58,020 --> 00:01:02,940 And then when you interact with the prompt, it should close the door and the prompt should reappear 18 00:01:02,940 --> 00:01:03,570 again. 19 00:01:03,570 --> 00:01:08,310 So I would like for you to take a few minutes of your time to go ahead and work on this project, and 20 00:01:08,310 --> 00:01:12,420 if you're not able to do it on your own quite yet, don't worry, I'm going to show you my solution 21 00:01:12,420 --> 00:01:13,050 next. 22 00:01:13,050 --> 00:01:18,000 But please take a few minutes to attempt to try to create this system. 23 00:01:18,000 --> 00:01:18,540 All right. 24 00:01:18,540 --> 00:01:20,520 Hopefully you got your system working. 25 00:01:20,520 --> 00:01:25,410 If you didn't, we're going to walk through the project together so you can see how I would approach 26 00:01:25,410 --> 00:01:25,710 it. 27 00:01:25,710 --> 00:01:30,540 So we need to control all of the doors on our game in this folder called doors. 28 00:01:30,540 --> 00:01:34,110 So let's create a server script in Server Script service. 29 00:01:34,110 --> 00:01:38,880 And this script is going to be responsible for handling all the doors and listening for when their proximity 30 00:01:38,880 --> 00:01:40,050 prompts are triggered. 31 00:01:40,050 --> 00:01:44,440 So we can call this script something like our door service. 32 00:01:45,670 --> 00:01:51,490 Inside of here, we're going to need a reference to replicated storage so we can get our better tween 33 00:01:51,490 --> 00:01:52,450 module. 34 00:01:52,480 --> 00:01:55,360 So we'll do game get service replicated storage. 35 00:01:55,360 --> 00:01:58,960 And then let's go ahead and require our better tween module. 36 00:01:58,960 --> 00:02:02,830 So we're going to do require replicated storage dot better tween. 37 00:02:02,860 --> 00:02:08,970 Now what we need to do is we basically need to loop through all of the doors inside of the doors folder, 38 00:02:08,970 --> 00:02:18,300 so we could do for every single door in Ipairs we're going to do workspace dot doors Getchildren. 39 00:02:18,300 --> 00:02:23,370 So we'll grab all of the doors in our game, and we need to listen to each one of these doors. 40 00:02:23,370 --> 00:02:24,570 Proximity prompts. 41 00:02:24,570 --> 00:02:29,790 So all the doors on our game, the proximity prompt is located in the frame. 42 00:02:30,270 --> 00:02:38,380 So what we can go ahead and do is we can do door dot frame, dot proximity prompt dot triggered. 43 00:02:38,650 --> 00:02:41,800 And we need to connect a function to this event. 44 00:02:41,800 --> 00:02:44,650 And we need to basically tween the door. 45 00:02:44,770 --> 00:02:50,920 Now to save a little bit on memory, I'm going to create a function outside of my for loop. 46 00:02:50,920 --> 00:02:53,950 I'll just call it on prompt triggered. 47 00:02:55,140 --> 00:03:01,230 That way I can call this function from within this lambda function and pass some arguments to it. 48 00:03:01,230 --> 00:03:07,260 That way if there's, let's say hundreds or thousands of doors in my game, they can all refer or call 49 00:03:07,260 --> 00:03:08,340 this one function. 50 00:03:08,340 --> 00:03:14,100 And I don't need to create thousands of functions for every single door in my game. 51 00:03:14,100 --> 00:03:19,350 Through each iteration of this loop, I'm instead going to refer to one function I create out here. 52 00:03:19,350 --> 00:03:25,460 So what we need to do is we need to check to see what the current key frame of the door is. 53 00:03:25,460 --> 00:03:28,910 If the door is key, frame is at its original key frame. 54 00:03:28,910 --> 00:03:30,980 That means we need to open the door. 55 00:03:30,980 --> 00:03:36,110 Otherwise, if the door is not at its original key frame, we need to close the door. 56 00:03:36,110 --> 00:03:44,180 So inside of our for loop, let's create a variable called original key frame and we need to tween the 57 00:03:44,180 --> 00:03:50,240 door part inside of our sliding door, as that is what all of our parts are welded to. 58 00:03:50,240 --> 00:03:51,830 They're all welded to this door part. 59 00:03:52,400 --> 00:03:57,650 So the original keyframe is going to be my door dot door dot keyframe. 60 00:03:57,650 --> 00:04:03,170 And then when my prompt is triggered, let's just go ahead and pass our door to this function as well 61 00:04:03,170 --> 00:04:04,910 as the original keyframe. 62 00:04:05,720 --> 00:04:08,710 And then this function of course is going to get those two arguments. 63 00:04:08,710 --> 00:04:11,530 So it's going to be the door and the original keyframe. 64 00:04:11,740 --> 00:04:16,810 And when the prompt is triggered, what we need to do is we need to disable the prompt so we can do 65 00:04:16,810 --> 00:04:21,430 door dot frame dot proximity prompt dot enabled. 66 00:04:21,430 --> 00:04:23,290 We'll set that equal to false. 67 00:04:23,470 --> 00:04:27,160 Then we need to create a tween info for the door. 68 00:04:27,160 --> 00:04:31,150 So tween info is equal to tween info dot new. 69 00:04:31,180 --> 00:04:35,580 You could have designed your tween info however you want and pass whatever you want, but for this demonstration, 70 00:04:35,580 --> 00:04:39,480 I'm just going to have a tween of two seconds and leave the rest at the defaults. 71 00:04:39,480 --> 00:04:44,640 And then what we need to do is we need to have an if statement to check to see if the door's door part 72 00:04:44,640 --> 00:04:49,410 dot c frame is equal to the original C frame. 73 00:04:49,770 --> 00:04:54,480 If it is, then we need to open the door. 74 00:04:54,840 --> 00:04:59,250 Otherwise, if it's not equal to the original C frame, we need to close the door. 75 00:04:59,640 --> 00:05:02,370 So this is where we get to use our better tween module. 76 00:05:02,370 --> 00:05:04,290 We're going to call the tween function. 77 00:05:04,290 --> 00:05:11,100 And we need to pass our instance which is going to be door dot door the tween info which we just created. 78 00:05:11,550 --> 00:05:12,810 And then the goal. 79 00:05:12,810 --> 00:05:15,450 So the goal will make a table. 80 00:05:15,450 --> 00:05:20,400 Here we need to tween the C frame of this part. 81 00:05:20,400 --> 00:05:28,190 And that C frame is going to have to be the original C frame, offset by some kind of value in a particular 82 00:05:28,190 --> 00:05:31,670 direction on, let's say I think the x axis. 83 00:05:31,700 --> 00:05:38,420 Because if we look at our door here, right, this is the door on the x axis or its local x axis. 84 00:05:38,420 --> 00:05:43,430 So we need to tween the door basically that way on its local axis. 85 00:05:43,490 --> 00:05:49,530 Now in your solution you might have used a vector three here and would have been like, okay, let me 86 00:05:49,530 --> 00:05:53,760 just tween it by um, some kind of amount on the x axis. 87 00:05:53,760 --> 00:05:58,950 The proper way to do this would be to get the size of the door on its, uh, x axis, so I could do. 88 00:05:58,980 --> 00:05:59,760 Door. 89 00:05:59,850 --> 00:06:00,570 Dot door. 90 00:06:00,600 --> 00:06:02,040 Dot size dot x. 91 00:06:02,040 --> 00:06:03,450 So let's just do that. 92 00:06:03,450 --> 00:06:09,540 And then otherwise if the door is not at the original C frame and we need to close it, let's just copy 93 00:06:09,540 --> 00:06:10,020 this. 94 00:06:10,030 --> 00:06:15,250 But this time let's just tween it to the original keyframe of the door. 95 00:06:15,700 --> 00:06:21,340 And then finally, we need to re-enable the prompt after the tween is finished. 96 00:06:21,340 --> 00:06:27,280 And if you remember, we are returning a number from this function that represents how long the tween 97 00:06:27,280 --> 00:06:28,210 lasts. 98 00:06:28,210 --> 00:06:29,770 So let's make a variable out here. 99 00:06:29,770 --> 00:06:32,200 Let's call it tween duration. 100 00:06:32,200 --> 00:06:37,140 And I'm not going to initialize it with any value because we're going to set it here. 101 00:06:37,140 --> 00:06:40,590 And we're also going to set it down here. 102 00:06:41,250 --> 00:06:44,190 And then we're going to yield for that duration. 103 00:06:44,190 --> 00:06:51,390 And after that duration is up we can go ahead and set the proximity prompt to be enabled again. 104 00:06:51,780 --> 00:06:52,170 Okay. 105 00:06:52,170 --> 00:06:55,140 So let's go ahead and test to see if our solution works. 106 00:06:55,140 --> 00:06:57,450 I'm going to play here with my player's character. 107 00:06:57,450 --> 00:07:00,580 And let's interact with the sliding door and look at that. 108 00:07:00,580 --> 00:07:04,060 It opens and it closes. 109 00:07:04,060 --> 00:07:04,600 Look at that. 110 00:07:04,600 --> 00:07:05,770 It's working good. 111 00:07:05,770 --> 00:07:10,480 But I want to kind of demonstrate a little bit of an issue to you. 112 00:07:10,750 --> 00:07:13,330 What if I rotate my door at an angle? 113 00:07:13,330 --> 00:07:16,630 Let's say my door is sloped at like 45 degrees, right. 114 00:07:17,410 --> 00:07:20,350 And I go and play the game and I want to open my door. 115 00:07:20,980 --> 00:07:22,060 Uh oh. 116 00:07:22,060 --> 00:07:23,360 What the heck? 117 00:07:23,390 --> 00:07:25,460 My door is not supposed to be doing that. 118 00:07:25,490 --> 00:07:26,900 Why is it moving like that? 119 00:07:27,500 --> 00:07:33,950 Well, the reason why the door is moving like this is because we are offsetting the C frame of this 120 00:07:33,950 --> 00:07:37,580 door on the global x axis. 121 00:07:37,580 --> 00:07:44,300 So the x axis in the game globally is this direction, which is why the door moves in that direction. 122 00:07:44,300 --> 00:07:51,070 Instead, what we need to do is we need to offset the C frame of our door by another C frame, and by 123 00:07:51,070 --> 00:07:56,860 doing so, instead of the door moving according to the global axes in the game, the door will move 124 00:07:56,860 --> 00:08:00,670 according to the local axes of our part. 125 00:08:00,850 --> 00:08:02,680 And I'll demonstrate that to you. 126 00:08:03,340 --> 00:08:09,670 Let me go back to my door here and let me select my door and let me go and select the move tool. 127 00:08:09,670 --> 00:08:15,290 So as you can see, these red arrows represent the local axes of my door. 128 00:08:15,860 --> 00:08:17,930 Now if I press control and l. 129 00:08:17,930 --> 00:08:22,310 Now I have swapped the move tool to be in the global axes mode. 130 00:08:22,310 --> 00:08:25,730 So these arrows now represent the global axes in the game. 131 00:08:25,730 --> 00:08:29,060 So as you can see, it makes sense why our door moved this way. 132 00:08:29,210 --> 00:08:32,990 But instead we want to move it according to its local axes. 133 00:08:32,990 --> 00:08:34,820 So the door moves this way. 134 00:08:35,570 --> 00:08:38,480 So to do that, let's go back to our door service. 135 00:08:38,480 --> 00:08:44,960 And instead of offsetting our keyframe by a vector three, we simply just need to offset it by a new 136 00:08:44,960 --> 00:08:47,660 keyframe just like that. 137 00:08:47,870 --> 00:08:55,430 And then if we go and play test the game and we go and interact with the door, oh no, we got an error. 138 00:08:55,430 --> 00:08:56,060 What? 139 00:08:56,060 --> 00:08:58,070 Invalid argument number two. 140 00:08:58,070 --> 00:09:00,620 Vector three expected got See? 141 00:09:00,650 --> 00:09:01,070 Frame. 142 00:09:01,070 --> 00:09:02,330 What is going on here? 143 00:09:02,450 --> 00:09:08,480 Well, the issue is we actually need to replace this addition symbol with a multiplication symbol. 144 00:09:08,510 --> 00:09:11,840 Now we are not multiplying the c frame. 145 00:09:11,840 --> 00:09:14,510 We are simply just adding this other c frame to it. 146 00:09:14,540 --> 00:09:20,540 The reason why we have to swap it from an addition symbol to a multiplication symbol is. 147 00:09:20,540 --> 00:09:22,730 That's just because I don't know. 148 00:09:22,730 --> 00:09:28,110 That's what the developers decided when they were making this game engine like 16, 18, 19 years ago. 149 00:09:28,110 --> 00:09:30,390 This is something you're just going to have to remember. 150 00:09:30,390 --> 00:09:35,730 If you want to offset a keyframe by another keyframe, you have to use a multiplication symbol. 151 00:09:35,730 --> 00:09:40,530 If you want to offset a keyframe by a vector three, you have to use an addition symbol. 152 00:09:40,530 --> 00:09:44,400 There really isn't, I don't think any good idea or reason for this. 153 00:09:44,400 --> 00:09:49,640 It's just this is what the developers chose when they created roadblocks like 19 years ago. 154 00:09:49,640 --> 00:09:50,960 So you're just going to have to remember that. 155 00:09:50,960 --> 00:09:58,520 Sorry, but now with a multiplication symbol, if we go and play the game and we interact with the sliding 156 00:09:58,520 --> 00:09:59,420 door, there we go. 157 00:09:59,420 --> 00:10:04,160 Now it works and the door moves according to the local axes. 158 00:10:04,190 --> 00:10:10,460 Alrighty, I hope you enjoyed this practice challenge and I also hope you learned a lot. 159 00:10:10,490 --> 00:10:14,000 Thank you for watching and I will see you in the next lecture.